using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlServerCe;
using System.Drawing; 
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace FeedFly
{
    public partial class EditFeedForm : Form, FolderBrowseInterface
    {
        private Feed _feed;
        private Form _senderForm;
        private int _folderId; 

        public EditFeedForm()
        {
            InitializeComponent();
        }

        public void PrepareForm(int feedId, string breadCrumbs, Form sender)
        {
            this.BreadCrumbLabel.Text = breadCrumbs;
            this.itemLimitTrackBar.Maximum = Settings.MaxItemCount;
            this._senderForm = sender;

            _feed = Feed.GetFeed(feedId);

            this.feedAddressTextBox.Text = _feed.Address;
            this.feedTitleTextBox.Text = _feed.Title;
            this.itemLimitTrackBar.Value = _feed.ItemLimit;
            this._folderId = _feed.FolderId;

            this.ItemLimitLabel.Text = this.itemLimitTrackBar.Value.ToString();
        }

        private void cancelMenuItem_Click(object sender, EventArgs e)
        {
            //  Will clear the error box if any before leaving this window
            errorLabel.Text = String.Empty;
            _senderForm.Show();
            Hide();
        }


        public void FolderChosen(int folderId, string folderName, string breadCrumbs)
        {
            if (this._folderId != folderId)
            {
                this._folderId = folderId;
                this.BreadCrumbLabel.Text = breadCrumbs;
            }
        }

        private void ChangeParent(object sender, EventArgs e)
        {
            Screens.SelectFolder.PrepareForm(this);
            Screens.SelectFolder.Show();
            Hide();
        }

        private void feedAddressTextBox_TextChanged(object sender, EventArgs e)
        {
            errorLabel.Text = String.Empty;
        }

        private void feedTitleTextBox_TextChanged(object sender, EventArgs e)
        {
            errorLabel.Text = String.Empty;
        }

        private void errorLabel_TextChanged(object sender, EventArgs e)
        {
            errorLabel.Visible = (errorLabel.Text.Length > 0);                 
        }

        private void itemLimitTrackBar_ValueChanged(object sender, EventArgs e)
        {
            ItemLimitLabel.Text = itemLimitTrackBar.Value.ToString();
        }

        private void CopyMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(feedAddressTextBox.SelectedText);
        }

        private void PasteMenuItem_Click(object sender, EventArgs e)
        {
            IDataObject clipObject = Clipboard.GetDataObject();
            if (clipObject.GetDataPresent(DataFormats.Text))
                ((TextBox)sender).Text = clipObject.GetData(DataFormats.Text).ToString();                
        }

        private void saveFeedMenuItem_Click(object sender, EventArgs e)
        {
            this.errorLabel.Text = String.Empty;

            if (feedAddressTextBox.Text.Length == 0)
            {
                this.errorLabel.Text = "Feed address is required.";
                return;
            }

            if (feedTitleTextBox.Text.Length == 0)
            {
                this.errorLabel.Text = "Feed title is required.";
                return;
            }

            Uri feedUri;

            if (!Uri.TryCreate(feedAddressTextBox.Text, UriKind.Absolute, out feedUri))
            {
                this.errorLabel.Text = "Not a valid web address.";
                return;
            }

            bool addressChanged = !this._feed.Address.Equals(feedAddressTextBox.Text
                , StringComparison.CurrentCultureIgnoreCase);

            if (addressChanged)
            {
                StringBuilder sql = new StringBuilder("SELECT 1 from Feed WHERE Address = '", 300);
                sql.Append(feedUri.ToString()).Append("'");

                if (Data.ExecuteScalar(sql.ToString()) != null)
                {
                    this.errorLabel.Text = "Feed address already exists.";
                    return;
                }
            }

            if (setDefaultLimitCheckBox.Checked == true)
                Settings.DefaultItemLimit = itemLimitTrackBar.Value.ToString();

            bool titleChanged = !this._feed.Title.Equals(feedTitleTextBox.Text);

            // if folder changed, attempt to refresh both folders
            // only the currently displayed folder will actually be fetched
            bool folderChanged = this._folderId == this._feed.FolderId;
            if(folderChanged)
            {
                Screens.Main.RefreshFolder(this._folderId);
                Screens.Main.RefreshFolder(this._feed.FolderId);
            }

            this._feed.Title = feedTitleTextBox.Text;
            this._feed.Address = feedAddressTextBox.Text;
            this._feed.ItemLimit = (short)itemLimitTrackBar.Value;
            this._feed.FolderId = this._folderId;

            this._feed.Update();

            // refresh feed titles on other screen if changed
            if (titleChanged)
            {
                Screens.Main.UpdateFeedTitle(this._feed);
                Screens.ItemList.UpdateFeedTitle(this._feed);
            }

            this._senderForm.Show();

            Hide();
        }
    }
}